/* 
    Purpose: Using the 1950-2010 Censuses,
             this file calculates 32 demographic 
             shares (by decade). Each share
             is a sex-race-education-age group.
             
             -Sex: male or female 
             -Race: black or white
             -Education: Has hs education or has not,
             -Age: range = 30-50; binned into 4 groups

    Note: 1950-2010 Censuses are used so that shares
          can be calculated for when Jacome et al.
          survey respondents are around 40.
          (e.g., 1910 survey respondents will receive 
          1950 Census shares, 1920 survey respondents will 
          receive 1960 Census shares.)
          These shares will be merged to survey respondents 
          in 2_Clean_PooledData.do.

    Creates: Census_shares_byrace_bysex_byedu_byage.dta
*/
clear
set more off
cd "$Mydirectory1/1_DataSources/CensusData/"


* Import raw data
    use ./input/Census_1910to2010_1pct_raw.dta, clear //download from IPUMS USA

    tab year, 
    drop if birthyr==9999 

* Keep relevant sample
    keep if age>=30 & age<=50
    keep if race<=2

* Dummy: HS education
    gen hs_ed = (educ>=6) if educ!=. 
    tab hs_ed, m

    gen age_bin =.
    replace age_bin =1 if inrange(age,30,35) 
    replace age_bin =2 if inrange(age,36,40)
    replace age_bin =3 if inrange(age,41,45)
    replace age_bin =4 if inrange(age,46,50)
    tab age_bin, m

forval i=1950(10)2010 {

    if `i'==1950 local weight "[aw=slwt]"
    if `i'!=1950 local weight "[aw=perwt]"

    preserve

    * Keep relevant sample
    keep if year==`i'
    
    * Get shares of each of 32 groups (2 sex x 2 race x 2 edu x 4 age bins)
    foreach group in white_male white_female nonw_male nonw_female {
        foreach ed_level in no_hsed hsed {
            foreach age_group in 1 2 3 4 {
                gen `group'_`ed_level'_`age_group'_census =.
            }
        }
    }

    lookfor _census
    local vars "`r(varlist)'"
    di "`vars'"

    foreach var of local vars {
        di "`var'"

    //white men
        if "`var'"=="white_male_no_hsed_1_census" {
            local r 1 
            local s 1
            local edu_l 0 
            local age_b 1           
        }
        if "`var'"=="white_male_no_hsed_2_census" {
            local r 1
            local s 1
            local edu_l 0
            local age_b 2            
        }
        if "`var'"=="white_male_no_hsed_3_census" {
            local r 1
            local s 1
            local edu_l 0
            local age_b 3           
        }
        if "`var'"=="white_male_no_hsed_4_census" {
            local r 1
            local s 1
            local edu_l 0
            local age_b 4           
        }
        if "`var'"=="white_male_hsed_1_census" {
            local r 1
            local s 1
            local edu_l 1
            local age_b 1          
        }
        if "`var'"=="white_male_hsed_2_census" {
            local r 1
            local s 1
            local edu_l 1
            local age_b 2           
        }
        if "`var'"=="white_male_hsed_3_census" {
            local r 1
            local s 1
            local edu_l 1
            local age_b 3           
        }
        if "`var'"=="white_male_hsed_4_census" {
            local r 1
            local s 1
            local edu_l 1
            local age_b 4           
        }

    //white women
        if "`var'"=="white_female_no_hsed_1_census" {
            local r 1 
            local s 2
            local edu_l 0 
            local age_b 1           
        }
        if "`var'"=="white_female_no_hsed_2_census" {
            local r 1
            local s 2
            local edu_l 0
            local age_b 2            
        }
        if "`var'"=="white_female_no_hsed_3_census" {
            local r 1
            local s 2
            local edu_l 0
            local age_b 3           
        }
        if "`var'"=="white_female_no_hsed_4_census" {
            local r 1
            local s 2
            local edu_l 0
            local age_b 4           
        }
        if "`var'"=="white_female_hsed_1_census" {
            local r 1
            local s 2
            local edu_l 1
            local age_b 1          
        }
        if "`var'"=="white_female_hsed_2_census" {
            local r 1
            local s 2
            local edu_l 1
            local age_b 2           
        }
        if "`var'"=="white_female_hsed_3_census" {
            local r 1
            local s 2
            local edu_l 1
            local age_b 3           
        }
        if "`var'"=="white_female_hsed_4_census" {
            local r 1
            local s 2
            local edu_l 1
            local age_b 4           
        }

    //black men
        if "`var'"=="nonw_male_no_hsed_1_census" {
            local r 2 
            local s 1
            local edu_l 0 
            local age_b 1           
        }
        if "`var'"=="nonw_male_no_hsed_2_census" {
            local r 2
            local s 1
            local edu_l 0
            local age_b 2            
        }
        if "`var'"=="nonw_male_no_hsed_3_census" {
            local r 2
            local s 1
            local edu_l 0
            local age_b 3           
        }
        if "`var'"=="nonw_male_no_hsed_4_census" {
            local r 2
            local s 1
            local edu_l 0
            local age_b 4           
        }
        if "`var'"=="nonw_male_hsed_1_census" {
            local r 2
            local s 1
            local edu_l 1
            local age_b 1          
        }
        if "`var'"=="nonw_male_hsed_2_census" {
            local r 2
            local s 1
            local edu_l 1
            local age_b 2           
        }
        if "`var'"=="nonw_male_hsed_3_census" {
            local r 2
            local s 1
            local edu_l 1
            local age_b 3           
        }
        if "`var'"=="nonw_male_hsed_4_census" {
            local r 2
            local s 1
            local edu_l 1
            local age_b 4           
        }

    //black women
        if "`var'"=="nonw_female_no_hsed_1_census" {
            local r 2 
            local s 2
            local edu_l 0 
            local age_b 1           
        }
        if "`var'"=="nonw_female_no_hsed_2_census" {
            local r 2
            local s 2
            local edu_l 0
            local age_b 2            
        }
        if "`var'"=="nonw_female_no_hsed_3_census" {
            local r 2
            local s 2
            local edu_l 0
            local age_b 3           
        }
        if "`var'"=="nonw_female_no_hsed_4_census" {
            local r 2
            local s 2
            local edu_l 0
            local age_b 4           
        }
        if "`var'"=="nonw_female_hsed_1_census" {
            local r 2
            local s 2
            local edu_l 1
            local age_b 1          
        }
        if "`var'"=="nonw_female_hsed_2_census" {
            local r 2
            local s 2
            local edu_l 1
            local age_b 2           
        }
        if "`var'"=="nonw_female_hsed_3_census" {
            local r 2
            local s 2
            local edu_l 1
            local age_b 3           
        }
        if "`var'"=="nonw_female_hsed_4_census" {
            local r 2
            local s 2
            local edu_l 1
            local age_b 4           
        }

            replace `var' =1 if (race==`r' & sex==`s' & hs_ed==`edu_l' & age_bin==`age_b') 
            
    }

    foreach var of local vars {
        di "`var'"

            replace `var' =0 if `var'==.
            tab `var',m 
            summ `var' `weight' 
            replace `var' = `r(mean)'
        }

            * Save one observation per Census decade
            keep year white_male_no_hsed_1_census-nonw_female_hsed_4_census
            keep if _n==1
            
            tempfile data`i'
            save `data`i''
            
            
            restore    
    }

        *Append all Census years
        use `data1950', clear
        append using `data1960'
        append using `data1970'
        append using `data1980'
        append using `data1990'
        append using `data2000'
        append using `data2010'
        
        rename year age40_racesex_eduage 
        
        compress
        save ./output/Census_shares_byrace_bysex_byedu_byage.dta, replace

